/*
 * 广告工具类
 */
import CryptoJs from 'crypto-js'

function getRandomString(length) {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  let result = ''
  for (let i = 0; i < length; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length)
    result += characters[randomIndex]
  }
  return result
}

function encrypt(text) {
  const key = CryptoJs.enc.Utf8.parse('Z2U0VJoLEAMVi1xoPWyDaQ==00000000')

  const encrypted = CryptoJs.AES.encrypt(text, key, {
    mode: CryptoJs.mode.ECB
  })

  return encrypted
}

function decrypt(text) {
  const key = CryptoJs.enc.Utf8.parse('Z2U0VJoLEAMVi1xoPWyDaQ==00000000')

  const decrypt = CryptoJs.AES.decrypt(text, key, {
    mode: CryptoJs.mode.ECB
  })

  return CryptoJs.enc.Utf8.stringify(decrypt)
}

export default {
  getRandomString,
  encrypt,
  decrypt
}
